<?php

//UMA new
namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Nikkei extends Model
{
    protected $guarded = [];

    /**
     * query scope
     *
     * @param mixed $query
     * @param mixed $date
     * @return query
     */
    public function scopeByDate($query, $date)
    {
        return $query->where('date', $date);
    }
    
    /**
     * ScopeLast
     *
     * @param  mixed $query
     * @return query builder
     */
    public function scopeLast($query)
    {
        return $query->whereDate('date', now()->subDay());
    }

    public function nikkei_score_rate() {
        return $this->belongsTo(NikkeiScoreRate::class, 'nikkei_rate_id')->withDefault();
    }


    public static function boot() {
        parent::boot();
        static::creating(function($model) {
            $model->nikkei_rate_id = NikkeiScoreRate::lessThanValue($model->change)->value('id');
        });
    }
}
